<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 2021/4/6
 * Time: 16:25
 */

namespace common\wodrow\wyiibackup;


use creocoder\flysystem\Filesystem;
use creocoder\flysystem\LocalFilesystem;
use wodrow\yii2wtools\tools\FileHelper;
use yii\base\Component;

class ConfigJobBackup extends Component
{
    public $name;
    public $from;
    public $configFiles;
    public $backupWaitUploadRoot = "@uploads_root";
    public $backupKeepRoot = "@yiiconfigbackup_root/yii_config";
    public $keep = 10;

    /**
     * @var Filesystem $master
     */
    protected $master;

    /**
     * @var LocalFilesystem $slave
     */
    protected $slave;

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        if (substr($this->backupWaitUploadRoot, 0, 1) === "@"){
            $this->backupWaitUploadRoot = \Yii::getAlias("{$this->backupWaitUploadRoot}/yii_config/{$this->name}");
        }
        if (substr($this->backupKeepRoot, 0, 1) === "@"){
            $this->backupKeepRoot = \Yii::getAlias("{$this->backupKeepRoot}/{$this->name}");
        }
        foreach ($this->configFiles as $k => $v) {
            if (substr($v, 0, 1) === "@"){
                $this->configFiles[$k] = \Yii::getAlias($v);
            }
        }
        $this->master = \Yii::createObject($this->from);
    }

    /**
     * @param string $params
     * @throws
     */
    public function backupKeep($params = "")
    {
        if ($this->keep > 0){
            $backDir = $this->backupKeepRoot;
            $backTDir = $backDir. DIRECTORY_SEPARATOR . YII_BT_TIME;
            if (!is_dir($backTDir))FileHelper::createDirectory($backTDir);
            $this->backup($backTDir, $params);
            $files = FileHelper::listDir($backDir, false);
            $_files = [];
            foreach ($files as $k => $v){
                $t = basename($v);
                if (!\common\components\Tools::isTimestamp($t)){
                    FileHelper::removeDirectory($v);
                }else{
                    $_files[$t] = $v;
                }
            }
            krsort($_files);
            $bl = array_slice($_files, 0, $this->keep);
            $rms = array_diff($_files, $bl);
            foreach ($rms as $k => $v) {
                FileHelper::removeDirectory($v);
            }
        }
    }

    /**
     * @param string $params
     * @throws
     */
    public function backupWaitUpload($params = "")
    {
        $this->backup($this->backupWaitUploadRoot, $params);
    }

    /**
     * @param string $params
     * @throws
     */
    public function backup($backupDir, $params = "")
    {
        $master = $this->master;
        $this->slave = \Yii::createObject([
            'class' => LocalFilesystem::class,
            'path' => $backupDir,
        ]);
        $files = [];
        foreach ($this->configFiles as $k => $v) {
            if ($master->has($v)){
                $vPath = $master->listContents($v);
                if ($vPath){
                    foreach ($vPath as $k1 => $v1) {
                        $type = $v1['type'];
                        $fromPath = $v1['path'];
                        if ($type != 'dir'){
                            $files[] = $fromPath;
                        }
                    }
                }else{
                    $files[] = $v;
                }
            }
        }
        foreach ($files as $k => $v){
            $c = $master->readStream("{$v}");
            if ($this->slave->has("{$v}")){
                $this->slave->updateStream($v, $c);
            }else{
                $this->slave->writeStream($v, $c);
            }
        }
    }
}